/*
 * Copyright 2006-2008 Michele Mostarda ( michele.mostarda@gmail.com ).
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.graphnav.aggregator.storage.impl.jenastorage;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import org.graphnav.aggregator.storage.Storage;
import org.graphnav.aggregator.storage.TripleIterator;
import org.graphnav.aggregator.storage.TripleFilter;

import java.io.File;
import java.net.URI;

/**
 * Defines a Jena storage implementation.
 */
public class JenaStorage implements Storage {

    private Model model;

    private File file;

    protected JenaStorage(Model m, File f) {
        model = m;
        file  = f;
    }

    protected File getFile() {
        return file;
    }

    protected Model getModel() {
        return model;
    }

    public void addTriple(URI sub, URI pred, URI obj) {
        model.add(
                model.createResource(sub.toString()),
                model.createProperty(pred.toString()),
                obj.toString()
        );
    }

    public void addTriple(URI sub, URI pred, String obj) {
        model.add(
                model.createResource(sub.toString()),
                model.createProperty(pred.toString()),
                obj
        );
    }

    public void removeTriple(URI sub, URI pred, URI obj) {
        Statement st = model.createStatement(
                model.createResource(sub.toString()),
                model.createProperty(pred.toString()),
                obj.toString()
        );
        model.remove(st);
    }

    public void removeTriple(URI sub, URI pred, String obj) {
        Statement st = model.createStatement(
                model.createResource(sub.toString()),
                model.createProperty(pred.toString()),
                obj
        );
        model.remove(st);
    }

    public boolean existsTriple(URI sub, URI pred, URI obj) {
        return model.contains(
                model.createResource(sub.toString()),
                model.createProperty(pred.toString()),
                obj.toString()
        );
    }

    public boolean existsTriple(URI sub, URI pred, String obj) {
        return model.contains(
                model.createResource(sub.toString()),
                model.createProperty(pred.toString()),
                obj
        );
    }

    public TripleIterator iterator(TripleFilter filter) {
        StmtIterator iter = model.listStatements();
        return new JenaTripleIterator(filter, iter);
    }
}
